package com.zx.Ding.common;


import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

//Spring来管理
@Component
@Slf4j
public class MyMetaObjecthandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {

        /**
         * 事务操作
         * 为了保证事务的一致性
         * 从单线程中取出存入的用户ID用于记录
         */
        metaObject.setValue("createTime", LocalDateTime.now());
        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("createUser",ThreadContext.getCurrentId());
        metaObject.setValue("updateUser",ThreadContext.getCurrentId());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        long thId = Thread.currentThread().getId();
        log.info("线程：{}",thId);
        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("updateUser",ThreadContext.getCurrentId());
    }
}
